#!/usr/bin/env python3
import ssl, socket, datetime
hostname = "example.com"
context = ssl.create_default_context()
with socket.create_connection((hostname, 443)) as sock:
    with context.wrap_socket(sock, server_hostname=hostname) as ssock:
        cert = ssock.getpeercert()
exp_date = datetime.datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
days_left = (exp_date - datetime.datetime.now()).days
if days_left < 30:
    print(f"警告！证书将在{days_left}天后过期")